package org.jackysoft.config;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.google.common.collect.Lists;

import freemarker.cache.TemplateLoader;
import freemarker.cache.WebappTemplateLoader;
import freemarker.template.ObjectWrapper;

@Configuration
@ComponentScan("org.jackysoft")
@EnableTransactionManagement
@EnableScheduling
public class CommonConfig implements  SchedulingConfigurer {
	protected final Log logger = LogFactory.getLog(this.getClass());

	@Resource
	protected ContextConfig propertyConfig;
	@Inject
	Environment env;
	private ObjectWrapper freemarkerObjectWrapper;
	private TemplateLoader templateLoader;

	public CommonConfig() {

	}

	@PostConstruct
	public void init() {
		

	}

	@PreDestroy
	public void destory() {

	}

	private ServletContext servletContext;
	private String freemarkerTemplateDirectory = "freemarker";

	public String getFreemarkerTemplateDirectory() {
		return this.freemarkerTemplateDirectory;
	}

	@Bean(destroyMethod = "shutdown")
	public Executor taskScheduler() {
		return Executors.newScheduledThreadPool(100);
	}

	@Bean
	TaskScheduler tasker() {
		return new ThreadPoolTaskScheduler();

	}

	@Bean
	public static PropertySourcesPlaceholderConfigurer propertyPlaceHolder()
			throws IOException {
		PropertySourcesPlaceholderConfigurer bean = new PropertySourcesPlaceholderConfigurer();
		PathMatchingResourcePatternResolver prp = new PathMatchingResourcePatternResolver();
		List<org.springframework.core.io.Resource> res = Lists.newLinkedList();
		res.add(prp.getResource("classpath:commons.properties"));
		res.add(prp.getResource("classpath:myapp.properties"));
		res.add(prp.getResource("classpath:test.properties"));
		bean.setLocations(res
				.toArray(new org.springframework.core.io.Resource[0]));
		return bean;
	}

	

	public @Bean
	ObjectWrapper freemarkerObjectWrapper() {
		this.freemarkerObjectWrapper = new freemarker.template.DefaultObjectWrapper();
		return this.freemarkerObjectWrapper;

	}

	public @Bean
	TemplateLoader templateLoader() {
		this.templateLoader = new WebappTemplateLoader(this.servletContext,
				propertyConfig.FMPATH);

		return this.templateLoader;
	}

	@Bean
	public JavaMailSender mailSender() {
		JavaMailSenderImpl jmail = new JavaMailSenderImpl();
		jmail.setHost(propertyConfig.mailHost);
		return jmail;
	}

	public String getContextUrl() {
		return System.getProperty("contextBase");
	}

	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		taskRegistrar.setScheduler(taskScheduler());

	}
}
